package com.GraduationDesign.backend.controller.user;

import com.GraduationDesign.backend.bl.user.AccountService;
import com.GraduationDesign.backend.po.User;
import com.GraduationDesign.backend.vo.ResponseVO;
import com.GraduationDesign.backend.vo.UserFormVO;
import com.GraduationDesign.backend.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: wzx
 * @date: 2022/2/27
 */
@RestController()
@RequestMapping("/api/user")
public class AccountController {
    private final static String ACCOUNT_INFO_ERROR = "用户名或密码错误";

    @Autowired
    private AccountService accountService;

    @PostMapping("/login")
    public ResponseVO login(@RequestBody UserFormVO userFormVO) {
        User user = accountService.login(userFormVO);
        if (user == null) {
            return ResponseVO.buildFailure(ACCOUNT_INFO_ERROR);
        }
        return ResponseVO.buildSuccess(user);

    }

    @PostMapping("/register")
    public ResponseVO registerAccount(@RequestBody UserVO userVO) {
        return accountService.registerAccount(userVO);
    }


    @GetMapping("/get_user_info")
    public ResponseVO getUserInfo(@RequestParam("userId") int id) {
        User user = accountService.getUserInfo(id);
        if(user==null){
            return ResponseVO.buildFailure(ACCOUNT_INFO_ERROR);
        }
        return ResponseVO.buildSuccess(user);
    }

    @PostMapping("/userInfo/update")
    public ResponseVO updateInfo(@RequestBody UserVO userVO,@RequestParam("userId") int id){
        return accountService.updateUserInfo(id,userVO.getPassword(),userVO.getUserName(),userVO.getPhoneNumber());

    }

    @PostMapping("/delete_user")
    public ResponseVO deleteUser(@RequestParam("userId") int userId){
        return accountService.deleteUser(userId);
    }
}
